<script setup name="name">
import { ref, reactive, watchEffect, defineEmits } from 'vue'
import {
  GetFinishedListAPI,
  GetTypeSelectAPI,
  GetAwmateriaListAPI,
  GetAccessoriesAPI,
  GetWasteAPI,
  GetHalfListAPI
} from '@/api/material/common'
const dictObj = ref([])
const { proxy } = getCurrentInstance()
// 封装 方法 字典查询
dictObj.value = proxy.useDict(props.dictKey || 'customer_complaints_order_type')
const props = defineProps({
  dictKey: {
    type: String || Number,
    default: 'customer_complaints_order_type'
  }
})

const emit = defineEmits(['selectsubmit', 'preservation', 'cancel', 'reset'])

const loading = ref(false)

const typeSelect = ref([])
// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10,
  materialName: '', // 名称
  materialCode: '', // 编码
  materialType: '', // 物料类别
  type: '3' // 类别
})

const handleSelect = ref([{ materialId: '' }])
const TableData = ref([])
const total = ref(0)

watchEffect(async () => {
  // console.log(query.type)
  query.materialType = ''
  const res = await GetTypeSelectAPI({ condingId: query.type })
  if (res.code !== 200) return
  typeSelect.value = res.theDrop
  if (query.type == 1) getList()
  if (query.type == 2) getList2()
  if (query.type == 3) getList3()
  if (query.type == 4) getList4()
  if (query.type == 5) getList5()
})

// 勾选数据
const handleSelectionChange = row => {
  handleSelect.value = row
}

// 是否禁用 保存
const d = computed(() => {
  // console.log(handleSelect.value.length > 0)
  return !handleSelect.value.length > 0
})

// 提交
const preservation = () => {
  const Obj = handleSelect.value
  Obj.bomDetailType = query.type
  Obj.bomMaterialId = Obj.materialId
  Obj.type = query.type
  emit('preservation', Obj)
}

// 重置
const reset = () => {
  for (let key in query) {
    query[key] = ''
  }
  getList()
}

// 物料改变
const get = () => {
  query.type = props.dictKey
  if (props.dictKey == 1) getList()
  if (props.dictKey == 2) getList2()
  if (props.dictKey == 3) getList3()
  if (props.dictKey == 4) getList4()
  if (props.dictKey == 5) getList5()
}

// 查询 原料
const getList = async () => {
  loading.value = true
  const res = await GetAwmateriaListAPI(query)
  if (res.code !== 200) return
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}

// 查询 辅料
const getList2 = async () => {
  loading.value = true
  const res = await GetAccessoriesAPI(query)
  if (res.code !== 200) return
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}

// 查询 半成品
const getList3 = async () => {
  loading.value = true
  const res = await GetHalfListAPI(query)
  if (res.code !== 200) return
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}

// 查询 成品
const getList4 = async () => {
  loading.value = true
  const res = await GetFinishedListAPI(query)
  if (res.code !== 200) return
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}

// 查询 废料
const getList5 = async () => {
  loading.value = true
  const res = await GetWasteAPI(query)
  if (res.code !== 200) return
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}
get()
</script>
<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form :inline="true" label-width="100px">
      <el-form-item label="物料类别">
        <el-select clearable v-model="query.materialType" placeholder="请选择类别">
          <el-option :label="item.label" :value="item.value" v-for="(item, index) in typeSelect" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="物料编码">
        <el-input clearable v-model.trim="query.materialCode" placeholder="输入物料编码" />
      </el-form-item>
      <el-form-item label="物料名称">
        <el-input clearable v-model.trim="query.materialName" placeholder="输入物料名称" />
      </el-form-item>
    </el-form>

    <el-divider />
    <el-table
      stripe
      border
      :loading="loading"
      :data="TableData"
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" width="80" type="index" />
      <el-table-column type="selection" width="55" />
      <!-- <el-table-column width="80">
        <template #default="scope">
          <el-radio-group v-model="handleSelect.materialId">
            <el-radio :label="scope.row.materialId" size="large">&nbsp;</el-radio>
          </el-radio-group>
        </template>
      </el-table-column> -->
      <el-table-column label="物料编码" prop="materialCode" width="auto" />
      <el-table-column label="物料名称" prop="materialName" width="auto" />
      <el-table-column label="规格型号" prop="specType" width="auto" />
    </el-table>
    <div class="pagination">
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="query.pageNum"
        v-model:limit="query.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 按钮 -->
    <el-row class="Btn">
      <el-button type="primary" @click="getList">查询列表</el-button>
      <el-button type="info" @click="reset">重置</el-button>
      <el-button type="success" @click="preservation" :disabled="d">保存</el-button>
      <el-button type="info" @click="emit('cancel')">取消</el-button>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.Btn {
  margin-top: 10px;
  float: right;
}
</style>
